package model;

import java.sql.*;
import java.util.*;

import myBeans.MenuInfo;

public class MenuRepository {
	private Connection conn;
	public MenuRepository(Connection conn){
		this.conn = conn;
	}
	
	public List<MenuInfo> GetMenuByUser(String UserID){
		List<MenuInfo> menus = new LinkedList<MenuInfo>();
		
		try
		{
			
			
			Statement stmt = conn.createStatement();
			String sql = "Select distinct t4.MenuID,t4.MenuName,t4.MenuURL, t4.Pagecall";
			sql += " From TBUser t1 inner join TBRoleUser t2 on t1.userid = t2.userid";
			sql += " inner join tbRoleMenu t3 on t2.roleid = t3.roleid";
			sql += " inner join tbmenu t4 on t3.menuid = t4.menuid";
			sql += " Where t1.UserID='" + UserID +"'  order by t4.MenuID";
			
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
					MenuInfo menuItem = new MenuInfo();
					menuItem.setMenuID(rs.getString("MenuID"));
					menuItem.setMenuName(rs.getString("MenuName"));
					menuItem.setMenuURL(rs.getString("MenuURL"));
					menuItem.setPageCall(rs.getString("Pagecall"));
					
					menus.add(menuItem);
				}				
			
			return menus;
		}
		catch(SQLException ex)
		{
			ex.printStackTrace();
		}
		
		return menus;
		
	}
	
	public List<MenuInfo> GetMenuDefault(){
		List<MenuInfo> menus = new LinkedList<MenuInfo>();
		
		try
		{
			
			Statement stmt = conn.createStatement();
			String sql = "Select distinct t4.MenuID,t4.MenuName,t4.MenuURL,t4.Pagecall ";
			sql += "From  tbRoleMenu t3  ";
			sql += "inner join tbmenu t4 on t3.menuid = t4.menuid ";  
			sql += "and t3.roleid = 'Default' ";
			
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				MenuInfo menuItem = new MenuInfo();
				menuItem.setMenuID(rs.getString("MenuID"));
				menuItem.setMenuName(rs.getString("MenuName"));
				menuItem.setMenuURL(rs.getString("MenuURL"));
				menuItem.setPageCall(rs.getString("Pagecall"));
				
				menus.add(menuItem);
			}				
			
			return menus;
		}
		catch(SQLException ex)
		{
			ex.printStackTrace();
		}
		
		return menus;
	}
}
